#!/usr/bin/python2.4


import constants
import db_object
import flags
from logger import logger
import rpc_stubs
import sql_query
import utils


flags.add_option('--log-test-results', '--lr', action='store_true')
flags.add_option('--no-limit-results-by-host', '--nolrh', action='store_false',
                 dest='limit_results_by_host', default=True)


class Inquirer(object):

  def __init__(self, test_plan):
    self.test_plan = test_plan

  def setup(self):
    pass

  def get_test_views_filter_data(self, **overrides):
    limit_results_by_host = overrides.pop(
        'limit_results_by_host', flags.get_option('limit_results_by_host'))
    filter_data = self.test_plan.get_test_views_filter_data(**overrides)
    if limit_results_by_host and self.test_plan.hosts:
      hostname__in = filter_data.get('hostname__in')
      if hostname__in:
        filter_data['hostname__in'] = list(set(hostname__in) &
                                           set(self.test_plan.hostnames))
      else:
        filter_data['hostname__in'] = list(self.test_plan.hostnames)
    return filter_data

  def get_detailed_test_views(self, **filter_data):
    filter_data = self.get_test_views_filter_data(**filter_data)
    filter_data.setdefault('log_results', flags.get_option('log_test_results'))
    test_views = rpc_stubs.tko.get_detailed_test_views(**filter_data)
    test_views = [db_object.DbObject(test_view) for test_view in test_views]
    return test_views

  def get_test_views_with_labels(self, **filter_data):
    filter_data = self.get_test_views_filter_data(**filter_data)
    filter_data.setdefault('log_results', flags.get_option('log_test_results'))
    if filter_data.get('include_labels'):
      logger.error('Arg include_labels is not supported for'
                   ' get_test_views_with_labels')
      return None
    sort_by = filter_data.pop('sort_by', None)
    filter_data['exclude_labels'] = list(filter_data.get('exclude_labels', []))
    all_test_views = []

    for label in constants.ALL_TL_LABELS:
      if label not in filter_data['exclude_labels']:
        filter_data['include_labels'] = [label]
        test_views = rpc_stubs.tko.get_test_views(**filter_data)
        for test_view in test_views:
          test_view['labels'] = [label]
        all_test_views += test_views
        filter_data['exclude_labels'].append(label)

    filter_data.pop('include_labels', None)
    unlabeled_test_views = rpc_stubs.tko.get_test_views(**filter_data)
    for test_view in unlabeled_test_views:
      test_view['labels'] = []
    all_test_views += unlabeled_test_views

    if sort_by:
      utils.sort_dicts(all_test_views, sort_by)
    all_test_views = [db_object.DbObject(test_view)
                      for test_view in all_test_views]
    return all_test_views

  def get_sql_test_views(self, **filter_data):
    filter_data = self.get_test_views_filter_data(**filter_data)
    filter_data.setdefault('log_results', flags.get_option('log_test_results'))
    return sql_query.sql.get_test_views(**filter_data)

  def get_sql_job_host_runs(self, **filter_data):
    filter_data = self.get_test_views_filter_data(**filter_data)
    filter_data.setdefault('log_results', flags.get_option('log_test_results'))
    return sql_query.sql.get_job_host_runs(**filter_data)
